Day 16 - Regular expressions - Multiple matches

53

$ grep -Eo "D[a-z]+" examples.txt

Dug

Dog

matches the letter D followed by at least one lowercase letter, so the output contains both Dug and

Dog (pay attention to the -o option used here to clearly show the matches). The code

$ grep -Eo "D[a-z]*" examples.txt

Dug

Dog

D

instead, also matches the single D that comes from the line R2-D2, as that D is actually followed by

zero or more lowercase letters. Pay attention to the last two examples: as you can see the + and the

* are applied to the last component of the regular expression, as happened for the braces, and not to

the last character. The syntax a+ works because a single letter or digit is also a regular expression

component on its own.

Exercises

Exercise 16.01

Match all the occurrences of exactly three digits in the file examples.txt

Go to solution

Exercise 16.02

The log file simple.log contains the status of HTTP responses, which is a three digit number preceded

and followed by a space. HTTP statuses are categorised according to the initial digit and 4xx

responses (that is 400, 401, 402, and so on) are considered resource errors. Count how many HTTP

4xx responses are in the file for each type of error.

Go to solution

Exercise 16.03

The log file simple.log contains the IP address of the client for each request. IP addresses are made of

four numbers separated by dots (i.e. A.B.C.D), where each number goes from 0 to 255 (thus having

from 1 to 3 digits). Find the 5 IP addresses that occur the highest number of times in the file, counting

them

Go to solution